package com.linkoog.devtools.archive.filesystem.gz;

import com.linkoog.devtools.archive.filesystem.ArchiveHandlerBase;
import com.intellij.util.io.FileAccessorCache;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Objects;

/**
 */
public class GZFileHandler extends ArchiveHandlerBase<GZFile> {

    public GZFileHandler(@NotNull String path) {
        super(path);
    }

    @Override
    public FileAccessorCache<GZFileHandler, GZFile> getFileAccessor() {
        return fileAccessor;
    }

    private static final FileAccessorCache<GZFileHandler, GZFile> fileAccessor = new FileAccessorCache<GZFileHandler, GZFile>(20, 10) {

        @Override
        protected GZFile createAccessor(GZFileHandler key) throws IOException {
            setFileAttributes(key, key.getCanonicalPath());
            return new GZFile(key.getFile().getCanonicalFile());
        }

        @Override
        public boolean isEqual(GZFileHandler val1, GZFileHandler val2) {
            return Objects.equals(val1, val2);
        }

        @Override
        protected void disposeAccessor(@NotNull GZFile fileAccessor) throws IOException {
            fileAccessor.close();
        }

    };

}